//===============================================================================
// Microsoft patterns & practices
// Web Client Software Factory
//===============================================================================
// Copyright � Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Collections.Generic;

using Microsoft.Practices.CompositeWeb.Web.UI;
using Microsoft.Practices.ObjectBuilder;

using MVPComposibility.ContactManager.DomainModel;
using MVPComposibility.ContactManager.Views.ContactDetails;

namespace WebApplication.ContactManager
{
	public partial class ContactDetails : UserControl, IContactDetailsView
	{
		const string AddressesViewStateProperty = "ContactDetails_Addresses";

		public event EventHandler Changed;

		private void FireChanged()
		{
			if (Changed != null)
			{
				Changed(this, EventArgs.Empty);
			}
		}

		public void lblTime_Load(object sender, EventArgs e)
		{
			lblTime.Text = DateTime.Now.ToString();
		}

		private ContactDetailsPresenter _presenter;

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				_presenter.OnViewInitialized();
			}
			_presenter.OnViewLoaded();
		}

		[CreateNew]
		public ContactDetailsPresenter Presenter
		{
			get
			{
				return _presenter;
			}
			set
			{
				_presenter = value;
				_presenter.View = this;
			}
		}

		protected void SaveButton_Click(object sender, EventArgs e)
		{
			OnSaveChanges(e);
		}

		protected void DiscardButton_Click(object sender, EventArgs e)
		{
			OnDiscardChanges(e);
		}

		#region IContactDetailsView Members

		public int ContactId
		{
			get
			{
				int id;
				if (!int.TryParse(IdLabel.Text, out id))
					id = -1;
				return id;
			}
			set
			{
				IdLabel.Text = value.ToString();
				FireChanged();
			}
		}

		public string ContactName
		{
			get
			{
				return NameTextBox.Text;
			}
			set
			{
				NameTextBox.Text = value;
				FireChanged();
			}
		}

		public IList<Address> Addresses
		{
			get
			{
				return (List<Address>)ViewState[AddressesViewStateProperty];
			}
			set
			{
				List<Address> addresses = new List<Address>(value);

				ViewState[AddressesViewStateProperty] = addresses;
				AddressesGridView.DataSource = addresses;
				AddressesGridView.DataBind();
				FireChanged();
			}
		}

		public event EventHandler SaveChanges;

		public event EventHandler DiscardChanges;

		#endregion

		protected virtual void OnSaveChanges(EventArgs e)
		{
			if (SaveChanges != null)
			{
				SaveChanges(this, e);
			}
		}

		protected virtual void OnDiscardChanges(EventArgs e)
		{
			if (DiscardChanges != null)
			{
				DiscardChanges(this, e);
			}
			FireChanged();
		}
	}
}